<?php

namespace App\Http\Service;

use App\Models\Searchhistory;
use App\Http\Service\BaseService;
use App\Http\Service\ChatgptService;
class SearchhistoryService  extends BaseService
{
    private $model;

    public function __construct(Searchhistory $model)
    {
        parent::__construct();
        $this->model = $model;

       
    }


    public function getList($request,$data=[])
    {
        $page = intval($request->input('page')) ?$request->input('page') :$this->cpage;
        $limit = intval($request->input('limit')) ?$request->input('limit') :$this->climit;
        $offset = ($page-1)*$limit;
        $where = [];
        if(isset($data['type'])&&!empty($data['type'])){
            $where[]=['type','=',$data['type']];
        }
        

        $fields = ['id','keyword','update_time'];
        $list = $this->model->select($fields)
        ->where($where)
        ->limit($limit)
        ->orderBy('update_time', 'desc')
        ->orderBy('num', 'desc')
        ->get();

        $result = [
            'limit'=> $limit,
            'data'=> $list
        ];
        return $result; 

    }
   
    public function addDate($request,$data=[])
    {
       
        $where=[];
        $where=[
            ['keyword','=',$data['keyword']],
            ['type','=',$data['type']],
            ['people_id','=',$data['people_id']],
        ];

        $first=$this->model->where($where)->first();
        if(!$first){
            $data['create_time']=date("Y-m-d H:i:s");
            $data['update_time']=date("Y-m-d H:i:s");
            $data['num']=1;
            $insertId=$this->model->insertGetId($data);  
        }else{
            $update_time=date("Y-m-d H:i:s");
            $num=$first['num'];
            $num=$num+1;
            $this->model->where($where)->update(['num'=>$num,'update_time'=>$update_time]);
        }
        
        return true;
        
        
    }
    //删除
    public function del($request,$data=[],$ids)
    {
        $where=[];
        $where=[
                ['people_id','=',$data['people_id']],
                ['type','=',$data['type']],
        ];
        #$this->getLastSql();
        $res = $this->model->where($where)->whereIn('id',$ids)->delete();
        if(!$res){
            $this->error('删除失败');
        }else{
            $this->success();
        }
    }   
}
